@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <div class="d-flex align-items-center gap-3">
                        <a href="{{ route('codes.index') }}" class="btn btn-sm btn-secondary">
                            <i class="fas fa-arrow-left"></i> 返回
                        </a>
                        <h5 class="mb-0">查看兑换码</h5>
                    </div>
                    <button class="btn btn-sm btn-success" onclick="exportCodes()">导出Excel</button>
                </div>

                <div class="card-body">
                    <h6 class="mb-3">活动：{{ $activity->name }}</h6>
                    
                    <div class="alert alert-info mb-3">
                        <div class="row">
                            <div class="col-md-4">
                                <strong>总数：</strong> {{ $total }} 条
                            </div>
                            <div class="col-md-4">
                                <strong>已使用：</strong> {{ $usedCount }} 条
                            </div>
                            <div class="col-md-4">
                                <strong>未使用：</strong> {{ $unusedCount }} 条
                            </div>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <form action="{{ route('codes.search-by-card') }}" method="GET" class="d-flex gap-2">
                            <input type="text" class="form-control" name="search" placeholder="请输入兑换卡编号" value="{{ $searchTerm ?? '' }}">
                            <button type="submit" class="btn btn-primary">搜索</button>
                            @if($searchTerm)
                                <a href="{{ route('codes.show', $activity->id) }}" class="btn btn-secondary">清除搜索</a>
                            @endif
                        </form>
                    </div>
                    
                    <div class="table-responsive">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>兑换码</th>
                                    <th>卡号</th>
                                    <th>状态</th>
                                    <th>过期时间</th>
                                    <th>使用时间</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($codes as $code)
                                <tr @if($searchTerm && str_contains($code->card_number, $searchTerm)) class="table-warning" @endif>
                                    <td>{{ $code->code }}</td>
                                    <td>{{ $code->card_number }}</td>
                                    <td>
                                        @if($code->is_used)
                                            <span class="badge bg-secondary">已使用</span>
                                        @else
                                            <span class="badge bg-success">未使用</span>
                                        @endif
                                    </td>
                                    <td>{{ $code->expired_at }}</td>
                                    <td>{{ $code->is_used ? $code->updated_at : '-' }}</td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="5" class="text-center">
                                        @if($searchTerm)
                                            未找到匹配的兑换码
                                        @else
                                            暂无数据
                                        @endif
                                    </td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>

                        <div class="d-flex justify-content-center mt-3">
                            <nav aria-label="Page navigation">
                                <ul class="pagination">
                                    {{-- 上一页 --}}
                                    <li class="page-item {{ $codes->onFirstPage() ? 'disabled' : '' }}">
                                        <a class="page-link" href="{{ $codes->appends(['search' => $searchTerm])->previousPageUrl() }}" {{ $codes->onFirstPage() ? 'tabindex="-1" aria-disabled="true"' : '' }}>
                                            上一页
                                        </a>
                                    </li>

                                    {{-- 首页 --}}
                                    @if($codes->currentPage() > 3)
                                        <li class="page-item">
                                            <a class="page-link" href="{{ $codes->appends(['search' => $searchTerm])->url(1) }}">1</a>
                                        </li>
                                        @if($codes->currentPage() > 4)
                                            <li class="page-item disabled">
                                                <span class="page-link">...</span>
                                            </li>
                                        @endif
                                    @endif

                                    {{-- 当前页附近的页码 --}}
                                    @foreach(range(max(1, $codes->currentPage() - 2), min($codes->lastPage(), $codes->currentPage() + 2)) as $page)
                                        <li class="page-item {{ $page == $codes->currentPage() ? 'active' : '' }}">
                                            <a class="page-link" href="{{ $codes->appends(['search' => $searchTerm])->url($page) }}">{{ $page }}</a>
                                        </li>
                                    @endforeach

                                    {{-- 末页 --}}
                                    @if($codes->currentPage() < $codes->lastPage() - 2)
                                        @if($codes->currentPage() < $codes->lastPage() - 3)
                                            <li class="page-item disabled">
                                                <span class="page-link">...</span>
                                            </li>
                                        @endif
                                        <li class="page-item">
                                            <a class="page-link" href="{{ $codes->appends(['search' => $searchTerm])->url($codes->lastPage()) }}">{{ $codes->lastPage() }}</a>
                                        </li>
                                    @endif

                                    {{-- 下一页 --}}
                                    <li class="page-item {{ !$codes->hasMorePages() ? 'disabled' : '' }}">
                                        <a class="page-link" href="{{ $codes->appends(['search' => $searchTerm])->nextPageUrl() }}" {{ !$codes->hasMorePages() ? 'tabindex="-1" aria-disabled="true"' : '' }}>
                                            下一页
                                        </a>
                                    </li>
                                </ul>
                            </nav>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection 